package drds.global_transaction.image.image_praser;

import com.google.common.base.Joiner;
import drds.common.Author;
import drds.global_transaction.Authors;
import drds.global_transaction.image.Image;
import drds.global_transaction.image.UndoLog;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.JSQLParserException;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Author(name = Authors.LI_YANG)
@Slf4j
public class InsertIImagePraser extends ImagePraser
{
    private Object primaryKeyColumnValue;

    InsertIImagePraser(String orginSql, UndoLog undoLog)
    {
        this.originalSql = orginSql;
        this.undoLog = undoLog;
    }

    @Override
    public String getTableName() throws JSQLParserException
    {
        return SqlPraser.insertSentenceTableName(originalSql);
    }

    @Override
    public List<String> getColumnNameList() throws JSQLParserException
    {
        return SqlPraser.insertSentenceColumnNameList(originalSql);
    }

    @Override
    public String getWhereCondition()
    {
        return primaryKeyColumnName + "= " + primaryKeyColumnValue;
    }
    //

    public void setPrimaryKeyColumnValue(Object primaryKeyColumnValue)
    {
        this.primaryKeyColumnValue = primaryKeyColumnValue;
    }

    @Override
    public Image generateBeforeImage()
    {
        Image image = new Image();
        image.setSchemaName(schemaName);
        image.setTableName(tableName);
        image.setRecordList(new ArrayList<>());
        return image;
    }

    //
    @Override
    public Image generateAfterImage() throws SQLException, JSQLParserException
    {
        if (primaryKeyColumnValue == null)
        {
            throw new NullPointerException("primaryKeyColumnValue");
        }
        return getImage();
    }


    @Override
    public String getBeforeOrAfterImageSql() throws JSQLParserException, SQLException
    {
        List<String> columnNameList = SqlPraser.insertSentenceColumnNameList(originalSql);
        String primaryKeyColumnName = getPrimaryKeyColumnName(getTableName());
        StringBuilder sb = new StringBuilder("select ");
        sb.append(Joiner
                .on(",")
                .skipNulls()
                .join(columnNameList));
        sb.append(" from ");
        sb.append(getTableName());
        sb.append(" where  1=1 ");
        List<String> columnValueList = SqlPraser.insertSentenceColumnValueList(originalSql);
        if (columnNameList.contains(primaryKeyColumnName))
        {
            primaryKeyColumnValue = columnValueList.get(columnNameList.indexOf(this.primaryKeyColumnName));
            sb.append(" and " + this.primaryKeyColumnName + "=" + primaryKeyColumnValue);
            return sb.toString();
        } else
        {
            for (String columnName : columnNameList)
            {
                sb.append(" and " + columnName + "=" + columnValueList.get(columnNameList.indexOf(columnName)));
            }
            return sb.toString();
        }
    }


}
